package com.fly.lambda.framework.web.interceptor;

import com.fly.lambda.framework.web.session.AccountSession;
import com.fly.lambda.framework.web.session.SessionManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Fly on 2016/7/19.
 */
public class AccountInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    SessionManage sessionManage;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean isAjax = request.getHeader("x-requested-with") != null; //判断是否ajax请求
        System.out.println(request.getRequestURI() + " isAjax: " + isAjax);

        AccountSession accountSession = sessionManage.getAccountSession(request);
        if (null != accountSession){
            return true;
        }
        else {
            response.sendRedirect(request.getContextPath() + "/manage/login");
            return false;
        }
    }
}
